﻿using MES.Desktop.Common;
using MES.Desktop.IService;
using MES.Desktop.SystemModule.Models;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MES.Desktop.SystemModule.ViewModels.Dialogs
{
    public class ModifyUserViewModel : DialogViewModelBase
    {
        public UserModel UserInfo { get; set; } =
            new UserModel();

        public DepartModel CurrentDepart { get; set; }
        public PostModel CurrentPost { get; set; }

        public ObservableCollection<DepartModel> Departs { get; set; } =
            new ObservableCollection<DepartModel>();
        public ObservableCollection<PostModel> Posts { get; set; } =
            new ObservableCollection<PostModel>();

        public bool IsReadOnlyUserName { get; set; }

        IUserService _userService;
        IDepartService _departService;
        IPostService _postService;
        public ModifyUserViewModel(IUserService userService,
            IDepartService departService,
            IPostService postService)
        {
            _userService = userService;
            _departService = departService;
            _postService = postService;
            UserInfo = new UserModel(_userService);
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            // 初始化部门和职位信息
            // 请求部门
            var departs = _departService.GetAllDeparts().ToList();
            for (int i = 0; i < departs.Count; i++)
            {
                var depart = departs[i];

                DepartModel dm = new DepartModel();
                dm.DepartId = departs[i].DepartId;
                dm.DepartName = departs[i].DepartName;

                Application.Current.Dispatcher.Invoke(() =>
                {
                    Departs.Add(dm);
                });
            }

            // 请求职位
            var posts = _postService.GetAllPosts().ToList();
            for (int i = 0; i < posts.Count; i++)
            {
                var depart = posts[i];

                PostModel pm = new PostModel();
                pm.PostId = posts[i].PostId;
                pm.PostName = posts[i].PostName;

                Application.Current.Dispatcher.Invoke(() =>
                {
                    Posts.Add(pm);
                });
            }

            var model = parameters.GetValue<UserModel>("model");
            if (model == null)
            {
                Title = "新增用户信息";

                
                UserInfo.UserName = "";
                UserInfo.Gender = 1;
                UserInfo.Password = "123456";
                UserInfo.UserIcon = "a01.jpg";

                if (Departs.Count > 0)
                    CurrentDepart = Departs[0];
                if (Posts.Count > 0)
                    CurrentPost = Posts[0];
            }
            else
            {
                Title = "编辑用户信息";

                IsReadOnlyUserName = true;

                UserInfo.UserId = model.UserId;
                UserInfo.UserName = model.UserName;
                UserInfo.RealName = model.RealName;
                UserInfo.Gender = model.Gender;
                UserInfo.Address = model.Address;
                UserInfo.Phone = model.Phone;
                UserInfo.Email = model.Email;
                UserInfo.Age = model.Age;
                UserInfo.Password = model.Password;
                //UserInfo.DepartId = model.DepartId;
                //UserInfo.DepartName = model.DepartName;
                //UserInfo.PostId = model.PostId;
                //UserInfo.PostName = model.PostName;

                if (Departs.Count > 0)
                    CurrentDepart = Departs.FirstOrDefault(d => d.DepartId == model.DepartId);
                if (Posts.Count > 0)
                    CurrentPost = Posts.FirstOrDefault(p => p.PostId == model.PostId);

                string[] temp = model.UserIcon.Split("/");
                UserInfo.UserIcon = temp[temp.Length - 1];
            }

        }

        public override void DoSave()
        {
            if (UserInfo.HasErrors) return;
            // 保存逻辑
            try
            {
                var count = _userService.UpdateUser(
                    new Entities.UserEntity
                    {
                        userId = UserInfo.UserId,
                        userName = UserInfo.UserName,
                        realName = UserInfo.RealName,
                        password = UserInfo.Password,
                        eIcon = UserInfo.UserIcon,
                        address = UserInfo.Address,
                        age = (int)UserInfo.Age,
                        status = 1,
                        phone = UserInfo.Phone,
                        gender = (int)UserInfo.Gender,
                        departId = CurrentDepart.DepartId,
                        departName = CurrentDepart.DepartName,
                        postId = CurrentPost.PostId,
                        postName = CurrentPost.PostName,
                    });
                if (count == 0)
                    throw new Exception("用户信息更新失败");

                // 保存成功后 ，需要关闭当前弹窗
                base.DoSave();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}
